﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;

namespace GWEB.WebFrame.Models
{
    public class SelectCourse
    {
        public PageInfo Init()
        {
            PageInfo pg = new PageInfo();
            pg.Type = "SelectCourse";
            pg.Title = "学员";                       
            pg.SMenuId = "JX02";        
            pg.Condition = "";
            pg.TableName = "cu_TeacherCourse";
            pg.KeyFieldName = "id";       
            HttpSessionState session = System.Web.HttpContext.Current.Session;
            pg.dict = BasePage.getDict(session);            
            pg.Sop = BasePage.Grant(session, pg.SMenuId);
            pg.Sop.ADD = pg.Sop.EDIT = pg.Sop.DEL = false;
            DataRow drUser = BasePage.GetUser(session);
            String leagueId = Fun.cString(drUser["LeagueId"]);

            String cid = "";
            HttpRequest request = System.Web.HttpContext.Current.Request;
            if (request["cid"] != null) cid = Fun.cSql(request["cid"]);

            String button = "<a href=\"javascript: Do('../Schedule/StudentClass/AddToCourse',{id:[id],CourseId:[CourseId],keyCode: GetKeyCode('[id]'),cid:'" + cid + "',signupClass:0})\"  class='layui-btn layui-btn-xs'>加入上课</a>"
                          + "<a href=\"javascript: Do('../Schedule/StudentClass/AddToCourse',{id:[id],CourseId:[CourseId],keyCode: GetKeyCode('[id]'),cid:'" + cid + "',signupClass:1})\"  class='layui-btn layui-btn-xs'>加入排队</a>"; ;

            pg.Sql = String.Format(@"select * from
(
select 
	id,classroomName,courseName,CourseId,
	ltrim(isnull(s0,0))+'/'+ ltrim(studentNumber) as studentNumber,
	ltrim(isnull(s1,0))+'/'+ ltrim(lineupNumber) as lineupNumber,
	convert(varchar(10),teachDate,120) as date,
    startTime+'-'+endTime as time,teacherName,'' link 
from cu_TeacherCourse t1
left join 
(
	select tid,
		sum(case when SignupClass=0 and state!=4 then 1 else 0 end) s0,
		sum(case when SignupClass=1 then 1 else 0 end) s1
	from cu_TeacherCourse_Sign
	group by tid
) t2 on t1.id=t2.tid
where TeachDate>=convert(varchar(10),getdate(),120) and leagueId={0} and t1.id not in (select tid from cu_TeacherCourse_Sign where cid={1})
) cu_TeacherCourse
where 1=1 ", leagueId,cid);
            ColumnInfo cl;
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "classroomName"; cl.HeadText = "教室";  pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Decimal; cl.FieldName = "courseName"; cl.HeadText = "课程"; cl.IsCondition = true; cl.ConditionPosition = 1; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "date"; cl.HeadText = "上课日期"; cl.IsCondition = true; cl.ConditionPosition = 1; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "time"; cl.HeadText = "上课时间";pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "teacherName"; cl.HeadText = "主教老师"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "studentNumber"; cl.HeadText = "已报名/可报名"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "lineupNumber"; cl.HeadText = "已排队/可排队"; pg.AddColumn(cl);

            cl = new ColumnInfo(); cl.FieldName = "link"; cl.HeadText = "操作"; cl.IsSort = false; cl.Script = button; pg.AddColumn(cl);
            return pg;
        }
    }
}